package com.wzt.car.controller;

import com.wzt.car.service.ICarDetailService;
import com.wzt.car.domain.CarDetail;
import com.wzt.car.query.CarDetailQuery;
import com.wzt.base.util.AjaxResponse;
import com.wzt.base.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carDetail")
public class CarDetailController {
    @Autowired
    public ICarDetailService carDetailService;


    /**
     * 保存或修改
     * @param carDetail 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResponse addOrUpdate(@RequestBody CarDetail carDetail) {
        try {
            if ( carDetail.getId() != null) {
                carDetailService.editOne(carDetail);
            }
            else {
                carDetailService.addOne(carDetail);
            }
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 单个删除
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResponse delete(@PathVariable("id") Long id) {
        try {
                carDetailService.removeOne(id);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResponse delete(@RequestBody List<Long> ids) {
        try {
                carDetailService.removeMul(ids);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
    * 通过id获取
    * @param id
    * @return 
    */
    @GetMapping("/{id}")
    public AjaxResponse get(@PathVariable("id") Long id) {
        try {
            CarDetail carDetail =carDetailService.getOne(id);
            return AjaxResponse.oneResponse().setData(carDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 获取所有
    * @return
    */
    @GetMapping
    public AjaxResponse list() {

        try {
            List< CarDetail> data = carDetailService.getAll();
            return AjaxResponse.oneResponse().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageResult 分页对象
    */
    @PostMapping
    public AjaxResponse json(@RequestBody CarDetailQuery query) {
        try {
            PageResult<CarDetail> pageList = carDetailService.getPage(query);
            return AjaxResponse.oneResponse().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }
}